[Frida 3] Frida JS API 사용법 정리 - Part 1

[Frida 3] Frida JS API 사용법 정리 - Part 1

Lecture
Security
태그
frida
mobile hacking
public
완성
Y
생성일
Mar 18, 2024 05:48 AM
LectureName
Mobile Hacking (Android - Frida )

1. Frida JS API


1.1 Frida JS API

Firida에서는 애플리케이션 분석, 흐름조작을 위하여 다양한 API를 제공합니다. JS, C, Swift, GO 등이 존재하는데, 보편적으로 JS가 많이 쓰입니다.
Frida에서 제공하는 JS API를 사용하게 되면, 다양한 모듈과, 메서드를 이용하여 다양한 작업을 수행할 수 있습니다.
 
 

1.2 JS API Content

JS API가 지원하는 항목은 다음과 같습니다. (클릭하면 공식 DOCS로 이동)
  1. Runtime information
    1. Frida
    2. Script
  1. Process, Thread, Module and Memory
    1. Thread
    2. Process
    3. Module
    4. ModuleMap
    5. Memory
    6. MemoryAccessMonitor
    7. CModule
    8. ApiResolver
    9. DebugSymbol
    10. Kernel
  1. Data Types, Function and Callback
    1. Int64
    2. UInt64
    3. NativePointer
    4. ArrayBuffer
    5. NativeFunction
    6. NativeCallback
    7. SystemFunction
  1. Network
    1. Socket
    2. SocketListener
    3. SocketConnection
  1. File and Stream
    1. File
    2. IOStream
    3. InputStream
    4. OutputStream
    5. UnixInputStream
    6. UnixOutputStream
    7. Win32InputStream
    8. Win32OutputStream
  1. Database
    1. SqliteDatabase
    2. SqliteStatement
  1. Instrumentation
    1. Interceptor
    2. Stalker
    3. ObjC
    4. Java
  1. CPU Instruction
    1. Instruction
    2. X86Writer
    3. X86Relocator
    4. x86 enum types
    5. ArmWriter
    6. ArmRelocator
    7. ThumbWriter
    8. ThumbRelocator
    9. ARM enum types
    10. Arm64Writer
    11. Arm64Relocator
    12. AArch64 enum types
    13. MipsWriter
    14. MipsRelocator
    15. MIPS enum types
  1. Other
    1. Console
    2. Hexdump
    3. Shorthand
    4. Communication between host and injected process
    5. Timing events
    6. Garbage collection
 
 
➡️ 여기서 주로 사용되는 JAVA로 실습을 진행해볼 예정입니다.
 
 
 
 
 

2. Frida JS API - Process


2.1 Process?

Process 는 프로세스와 관련된 정보를 조회할 수 있는 객체입니다. 프로세스와 관련된 정보로는 PID, 페이지 등을 포함해 맵핑된 메모리를 조회할 수 있습니다. 
안드로이드는 애플리케이션 실행 시 메모리에 정보가 적재됨으로, 애플리케이션이 실행되고 적재된 메모리를 스캔할 수 있습니다.
 
 

2.1 Process method

기본 구조
함수
설명
Process.enumerateModules()
프로세스에 맵핑된 모든 모듈을 열거합니다.
Process.findModuleByAddress(address) Process.getModuleByAddress(address)
첫 번째 인자에 전달된 주소(address)가 어떤 모듈에 할당된 메모리인지 알아낼 수 있습니다.
Process.findModuleByName(name) Process.getModuleByName(name)
첫 번째 인자에 전달된 모듈 이름의 주소를 가져옵니다.
Process.findRangeByAddress(address) Process.getRangeByAddress(address)
전달된 메모리의 페이지 크기와 권한을 알아낼 수 있습니다.
 
예시
setTimeout(function() { // libfoo 모듈 탐색 let libfooModule = Process.getModuleByName("libfoo.so"); // 베이스 주소와 경로 탐색 console.log("modules Base: ", libfooModule.base); console.log("modules Path: ", libfooModule.path); // 베이스 주소로 탐색 let modulesInfo = Process.findRangeByAddress(ptr(libfooModule.base)) console.log("Protection: ", modulesInfo.protection); }, 2000);
 
 
 

3. Frida JS API - Thread


Thread는 현재 스레드에서 백트레이스를 출력할 수 있으며, 실행을 일시적으로 중지시킬 수 있는 객체입니다. 앱을 분석할 때 버그가 발생하거나 현재 실행 흐름을 파악할 때 주로 백트레이스를 확인하는데, 이때 사용할 수 있습니다.
함수
함수
설명
Thread.backtrace([context, backtracer])
현재 스레드의 백트레이스를 제공합니다.
Thread.sleep(delay)
현재 스레드의 실행을 중지하기 위해 사용되며, 초 단위를 사용합니다.
 
예시
const f = Module.getExportByName('libcommonCrypto.dylib', 'CCCryptorCreate'); Interceptor.attach(f, { onEnter(args) { console.log('CCCryptorCreate called from:\n' + Thread.backtrace(this.context, Backtracer.ACCURATE) .map(DebugSymbol.fromAddress).join('\n') + '\n'); } });
 
 
 

4. Frida JS API - Memory


Memory메모리와 관련된 객체이며, 애플리케이션에 대한 메모리의 할당, 데이터 검색, 복사, 패치, 권한 변경을 할 수 있습니다.
 
함수
함수
설명
Memory.scanSync(address, size, pattern)
특정 주소부터 원하는만큼 메모리를 스캔하여 임의의 바이트를 찾습니다. callback 을 등록하고 싶다면 Memory.scan(addr, size, pattern, callbacks) 를 사용합니다.
Memory.protect(address, size, protection)
메모리의 권한을 변경합니다.
Memory.alloc(size[, options])
프로세스의 힙 메모리 내의 전달된 크기만큼 메모리를 할당하고 주소를 NativePointer 형태로 반환합니다.
Memory.allocUtf8String(str)
프로세스의 힙 메모리 내의 Utf8 형태의 문자열을 할당하고 주소를 NativePointer 형태로 반환합니다.
Memory.readUtf8String(address, size)
메모리에서 UTF-8 문자열을 읽어올 때 사용됩니다.
  • address 는 메모리 주소 값을 말합니다. ( 0x ~ )
  • size 는 주소 값으로부터의 길이를 말합니다.
  • pattern 은 바이트 값으로, 12 34 56 78 형식으로 1byte 형태의 패턴으로 사용합니다.
 
 
 

5. Frida JS API - Module


Module프로세스의 주소를 알아내는 용도로 사용하는 객체입니다. 해당 객체를 사용하면 편리하게 프로세스의 베이스 주소, 외부 함수 주소를 가져올 수 있습니다.
 
함수
함수
설명
Module.findBaseAddress(name) Module.getBaseAddress(name)
인자로 전달된 모듈의 베이스 주소를 알아냅니다.
Module.findExportByName(moduleName|null, exportName) Module.getExportByName(moduleName|null, exportName)
외부 함수의 주소를 가져올 수 있습니다. 모듈 이름을 알 수 없는 경우 첫 번째 인자에 null을 전달합니다.
 
예시
let openModule = Module.findExportByName(null, "open"); console.log("open: " + openModule); // 0xffff ~ // 특정 라이브러리에서 모듈을 가져옴 let libc = "libc.so" let fopen = Module.findExportByName(libc, "fopen"); console.log("fopen in libc: " + fopen); // 0xffff ~